package com.benio.ycfdisplayer;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.benio.ycfdisplayer.fresco.FrescoView;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.GenericDraweeHierarchyInflater;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.interfaces.DraweeHierarchy;
import com.facebook.drawee.view.DraweeHolder;

/**
 * Fresco 自定义View
 * Created by zhangzhibin on 2016/8/31.
 */
public class CustomView extends View implements FrescoView {

    private DraweeHolder<DraweeHierarchy> mDraweeHolder;

    private static final String TAG = "FrescoView";

    private Drawable mDrawable;
    private boolean mInitialised = false;

    public CustomView(Context context) {
        super(context);
        init(context, null);
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (mInitialised) {
            return;
        }
        mInitialised = true;
        mDraweeHolder = DraweeHolder.create(null, context);
        GenericDraweeHierarchyBuilder builder =
                GenericDraweeHierarchyInflater.inflateBuilder(context, attrs);
        GenericDraweeHierarchy hierarchy = builder.build();
        setHierarchy(hierarchy);
    }

    public void setImageURI(Uri uri, @Nullable Object callerContext) {
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setCallerContext(callerContext)
                .setUri(uri)
                .setOldController(mDraweeHolder.getController())
                .build();
        setController(controller);
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        onDetach();
    }

    @Override
    public void onStartTemporaryDetach() {
        super.onStartTemporaryDetach();
        onDetach();
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        onAttach();
    }

    @Override
    public void onFinishTemporaryDetach() {
        super.onFinishTemporaryDetach();
        onAttach();
    }

    protected void onDetach() {
        mDraweeHolder.onDetach();
    }

    protected void onAttach() {
        mDraweeHolder.onAttach();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return mDraweeHolder.onTouchEvent(event) || super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mDrawable == null) {
            return;
        }
        mDrawable.setBounds(0, 0, getWidth(), getHeight());
        mDrawable.draw(canvas);
        //Log.d(TAG, "onDraw: " + getWidth() + "," + getHeight());
    }

    @Override
    protected boolean verifyDrawable(Drawable who) {
        if (who == mDraweeHolder.getTopLevelDrawable()) {
            return true;
        }
        return super.verifyDrawable(who);
    }

    //    @Override
    public Drawable getTopLevelDrawable() {
        return mDraweeHolder.getTopLevelDrawable();
    }

    public void setDrawable(Drawable drawable) {
        if (mDrawable != drawable) {
            mDrawable = drawable;
            if (mDrawable != null) {
                mDrawable.setCallback(this);
            }
            invalidate();
        }
    }

    @Override
    public DraweeController getController() {
        return mDraweeHolder.getController();
    }

    @Override
    public void setController(@Nullable DraweeController draweeController) {
        mDraweeHolder.setController(draweeController);
        setDrawable(mDraweeHolder.getTopLevelDrawable());
    }

    @Override
    public DraweeHierarchy getHierarchy() {
        return mDraweeHolder.getHierarchy();
    }

    @Override
    public void setHierarchy(DraweeHierarchy hierarchy) {
        mDraweeHolder.setHierarchy(hierarchy);
        setDrawable(mDraweeHolder.getTopLevelDrawable());
    }
}
